$(document).ready(function() {
	$('#page').hide();

	loadQCMData();

	$('#qcm_next').click(function() {
		if (numCurrentQuestion < nbQuestions - 1) {
			numCurrentQuestion++;

			numCurrentQuestionInSection++;

			if (numCurrentQuestionInSection >= nbQuestionsInCurrentSection) {
				numCurrentSection++;
				numCurrentQuestionInSection = 0;
				initNbQuestionsInCurrentSection();
			}

			saveNumCurrentQuestion();
			$('#question').fadeOut('fast', function() {
				displayQuestion();
			});
		}
	});

	$('#qcm_previous').click(function() {
		if (numCurrentQuestion > 0) {
			numCurrentQuestion--;

			numCurrentQuestionInSection--;

			if (numCurrentQuestionInSection < 0) {
				numCurrentSection--;
				initNbQuestionsInCurrentSection();
				numCurrentQuestionInSection = nbQuestionsInCurrentSection - 1;
			}

			saveNumCurrentQuestion();
			$('#question').fadeOut('fast', function() {
				displayQuestion();
			});
			

		}
	});
	
});

var questionHTML;
var answerHTML;
var dataJSON;
var numCurrentSection = 0;
var nbQuestionsInCurrentSection = 0;
var nbSections = 0;
var nbQuestions = 0;
var numCurrentQuestion = 0;
var numCurrentQuestionInSection = 0;
var results = new Array();

function loadHTML() {
	$.get('../template/question.html', function(data) {
		questionHTML = data;
		$.get('../template/answer.html', function(data) {
			answerHTML = data;
			displayQuestion();
		});
	});
}

function loadQCMData() {

	if (localStorage["QCM"] == null) {
		$.getJSON('../TestServlet?idTest=' + localStorage["idCurrentTest"], function(data) {
			dataJSON = data;
			saveQCM();
			initQCM();
		});
	} else {
		dataJSON = JSON.parse(localStorage["QCM"]);
		initQCM();
	}

}

function saveNumCurrentQuestion() {
	localStorage["numCurrentQuestion"] = JSON.stringify(numCurrentQuestion);
	localStorage["numCurrentSection"] = JSON.stringify(numCurrentSection);
	localStorage["numCurrentQuestionInSection"] = JSON
			.stringify(numCurrentQuestionInSection);
}

function initNumCurrentQuestion() {
	if (localStorage["numCurrentQuestion"] != null) {
		numCurrentQuestion = JSON.parse(localStorage["numCurrentQuestion"]);
		if (localStorage["numCurrentSection"] != null) {
			numCurrentSection = JSON.parse(localStorage["numCurrentSection"]);
			if (localStorage["numCurrentQuestionInSection"]) {
				numCurrentQuestionInSection = JSON
						.parse(localStorage["numCurrentQuestionInSection"]);
			}
		}
	}
}

function initQCM() {
	initNbSections();
	initNbQuestions();
	initNbQuestionsInCurrentSection();
	initNumCurrentQuestion();
	loadHTML();
}

function initNbQuestionsInCurrentSection() {
	nbQuestionsInCurrentSection = getCurrentSection().nbQuestions;
}

function initNbSections() {
	nbSections = dataJSON.sections.length;
}

function initNbQuestions() {
	nbQuestions = dataJSON.nbQuestions;
}

function setQuestionNumber() {
	$('#question_number').html((numCurrentQuestion + 1) + "/" + nbQuestions);
}

function getCurrentQuestion() {
	return getCurrentSection().questions[numCurrentQuestionInSection];
}

function getCurrentSection() {
	return dataJSON.sections[numCurrentSection];
}

function saveQCM(){
	localStorage["QCM"] = JSON.stringify(dataJSON);
}


function ckBoxMarquedChangeHandler(){
	if($('#ckBoxMarqued').is(':checked')){
		isMarked = true;
	}
	else{
		isMarked = false;
	}
	getCurrentQuestion().isMarked = isMarked;
	saveQCM();
}

function getCurrentQuestionId(){
	return getCurrentQuestion().id;
}

function addResult(result){
	results.push(result);
}

function removeResult(result){
	var cpt = 0;
	$.each(results,function(key,res){
		if(res.idQuestion == result.idQuestion && res.idAnswer == result.idAnswer)
			results.splice(cpt,1);
		cpt++;
	});
}

function ckBoxAnswerChangeHandler(){
	var id = $(this).parent().attr('id');
	var answerId = id.split('_')[1];
	var result = new Object();
	result.idQuestion = getCurrentQuestionId();
	result.idAnswer = answerId;
	result.idSession = localStorage["idCurrentSession"];
	
	if($(this).is(':checked')){
		addResult(result);
	}
	else{
		removeResult(result);
	}
	
	saveResults();
}

function saveResults(){
	localStorage["Results"] = JSON.stringify(results);
}

function displayQuestion() {
	
	$('#question').html(questionHTML);
	
	if(getCurrentQuestion().isMarked){
		$('#ckBoxMarqued').attr('checked',true);
	}
	
	$('#ckBoxMarqued').change(ckBoxMarquedChangeHandler);
	
	$('#question_text').html(dataJSON.sections[numCurrentSection].questions[numCurrentQuestionInSection].statement);
	setQuestionNumber();
	$('#qcm_title').html(dataJSON.name);
	$('#section_title').html(dataJSON.sections[numCurrentSection].subject);
	$('#section_number').html((numCurrentSection + 1) + '/' + nbSections);
	$.each(dataJSON.sections[numCurrentSection].questions[numCurrentQuestionInSection].answers,
					function(key, answer) {
						$('#answers').append(answerHTML);
						$('#answers fieldset:last-child').attr('id','answer_' + answer.id);
						$('#answer_' + answer.id + ' label').html(answer.statement);
						$('#answer_' + answer.id + ' input').change(ckBoxAnswerChangeHandler);
					});
	
	$('#page').show();
	$('#question').fadeIn('fast');
	
}